package com.tz.colawallet.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tz.colawallet.R;
import com.tz.colawallet.basecode.base.BaseFragment;
import com.tz.colawallet.bean.BannerBean;
import com.tz.colawallet.bean.MessageEvent;
import com.tz.colawallet.bean.Recommand;
import com.tz.colawallet.constant.Key;
import com.tz.colawallet.ui.activity.CommonWebViewActivity;
import com.tz.colawallet.ui.activity.PerfectMessageActivity;
import com.tz.colawallet.ui.adapter.MainFirstAdapter;
import com.tz.colawallet.ui.biz.MainFirstFragBiz;
import com.tz.colawallet.ui.component.DaggerMainFirstFragComponent;
import com.tz.colawallet.ui.contract.MainFirstFragContract;
import com.tz.colawallet.ui.module.MainFirstFragModule;
import com.tz.colawallet.ui.presenter.MainFirstFragPresenter;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class MainFirstFrag extends BaseFragment implements MainFirstFragContract.View {

    @Inject
    MainFirstFragPresenter presenter;
    @Inject
    MainFirstFragBiz biz;

    RecyclerView recyclerView;
    SwipeRefreshLayout firstSwipeRefreshLayout;
    TextView mainFirstMore;

    private MainFirstAdapter mainFirstAdapter;
    private List<Recommand> recommandList;
    private int clickPosition;
    private Banner banner;
    private List<BannerBean> bannerBeanList;

    @Override
    protected void setup() {
        super.setup();
        DaggerMainFirstFragComponent.builder().mainFirstFragModule(new MainFirstFragModule(this)).build().inject(this);
        presenter.setBiz(biz);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_main_first;
    }

    @Override
    protected void initTitle(View view) {

    }

    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((String) path).into(imageView);
        }
    }

    @Override
    protected void init(View view) {
        bannerBeanList = new ArrayList<>();
        banner = (Banner) view.findViewById(R.id.banner);
        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
//        banner.setImages(list_path);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播图的标题集合
//        banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Bundle bundle = new Bundle();
                bundle.putString(Key.WEB_URL, bannerBeanList.get(position).getUrl());
                switchActivity(CommonWebViewActivity.class, bundle);
            }
        });


        recyclerView = view.findViewById(R.id.recycler_view);
        firstSwipeRefreshLayout = view.findViewById(R.id.first_swipe_refresh_layout);
        mainFirstMore = view.findViewById(R.id.main_first_more);


        recommandList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainFirstAdapter = new MainFirstAdapter(R.layout.item_product, getActivity());
        recyclerView.setAdapter(mainFirstAdapter);

        firstSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reloadData();
                firstSwipeRefreshLayout.setRefreshing(false);
            }
        });

        mainFirstAdapter.setOnListener(new MainFirstAdapter.OnListener() {
            @Override
            public void onListener(int position) {
                clickPosition = position;
//                //0为已经写过详情，1为未写过
//                if (SPUtils.get(getActivity(), Key.MESSAGE_DETAIL, "1").equals("0")) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString(Key.WEB_URL, recommandList.get(position).getWapurl());
//                    switchActivity(CommonWebViewActivity.class, bundle);
//                } else {

///product/addApplyNum     参数  productId      更新申请人数
                presenter.addApplyNum(recommandList.get(position).getId());
                presenter.checkDetail();
//                switchActivity(PerfectMessageActivity.class, null);
//                }
            }
        });

//        mainFirstAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
////                mPage++;
////                getPresenter().getProcurementCluesList(false);
//            }
//        }, recycler_view);

        mainFirstMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setEventKey(Key.EVENT_TO_MAIN_SECOND);
                EventBus.getDefault().post(messageEvent);
            }
        });
        presenter.recommand();
        presenter.getBanner();
    }

    @Override
    protected void initData() {

    }

    private void reloadData() {
        presenter.recommand();
    }

    @Override
    public Context curContext() {
        return getActivity();
    }

    @Override
    public void showRecommand(List<Recommand> o) {
        recommandList.clear();
        recommandList.addAll(o);
        mainFirstAdapter.replaceData(recommandList);
    }

    @Override
    public void showBanner(List<BannerBean> o) {
        bannerBeanList.addAll(o);
        List<String> imgUrlList = new ArrayList<>();
        for (BannerBean bean : bannerBeanList) {
            imgUrlList.add(bean.getImgurl());
        }
        banner.setImages(imgUrlList);
        banner.start();
    }


    @Override
    public void showCheckDetail(Object o) {
        if (o.equals("0")) {
            Bundle bundle = new Bundle();
            bundle.putString(Key.WEB_URL, recommandList.get(clickPosition).getWapurl());
            switchActivity(CommonWebViewActivity.class, bundle);
        } else {
            switchActivity(PerfectMessageActivity.class, null);
        }
        reloadData();
    }

    @Override
    public void showMsg(String msg) {
        showToast(msg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {/* Do something */}
}
